<?php
/**
 * Created by PhpStorm.
 * User: lihao
 * Date: 2017-12-20
 * Time: 15:35
 * 用于添加订单信息
 */
   // header("Content-Type:text/html;charset=utf-8");
    $serverName = "bdm246562380.my3w.com";
    $userName = "bdm246562380";
    $password = "p081742lh";
    $dbName = "bdm246562380_db";

    /**
     * Class outobj
     * 定义返回结果对象
     */
    class outobj{
        const success = '';
        const message = '';
        const data = '';
    }
    $outObj = new outobj();

    /**
     *
     *  定义插入四个表格SQL执行的状态标志
     *  (NO 标记没有执行SQL，即没有数据)
     *  orderFlag
     *  chairFlag
     *  tableFlag
     *  teaTableFlag
     *  tvTableFlag
     */
    $orderFlag    = 'NO';
    $chairFlag    = 'NO';
    $tableFlag    = 'NO';
    $teaTableFlag = 'NO';
    $tvTableFlag  = 'NO';

//    $data2 = $GLOBALS['HTTP_RAW_POST_DATA'];
    $data2 = file_get_contents('php://input');
    $data = json_decode($data2);

//    echo json_encode($data,JSON_UNESCAPED_UNICODE);

    $customerName = $data->customerName;
//    $customerCode = $data->customerCode;
    $customerCode = "byjiasi";
    $customerPhone = $data->customerPhone;
    $deliverGoodsTime = $data->deliverGoodsTime;
    $freight = $data->freight;
    $freightAddress = $data->freightAddress;
    $freightPhone = $data->freightPhone;
    if($customerName==''||$customerPhone==''||$deliverGoodsTime==''|| $freightPhone==''){
        $outObj->success = false;
        $outObj->message = '有必填项为空！';
        $outObj->data = '';
       echo json_encode($outObj,JSON_UNESCAPED_UNICODE);
    } else {
        //备注
        $remarks = $data->otherInfo?$data->otherInfo[0]->remarks:'';

        //椅子信息
        $chairInfo =$data->chairInfo;

        //桌子信息
        $tableInfo = $data->tableInfo;

        //茶几信息
        $teaTableInfo = $data->teaTableInfo;

        //电视柜信息
        $tvTableInfo = $data->tvTableInfo;

        // 大理石桃面信息
        $desktopInfo = $data->desktopInfo;

        // 创建连接
        $conn = new mysqli($serverName, $userName, $password, $dbName);

        $firstSQL = "SELECT id FROM orderInfo";
        $firstResult = $conn->query($firstSQL);
        //订单数量额度
        $num = $firstResult->num_rows + 8000001;

        // 生成订单号，和创建订单时间
        $newTime = date('Y-m-d H:i:s', time());
        $orderId = 'BY' . date('Y', time()) . date('m', time()) . date('d', time()) . date('H', time()) . date('i', time()) . date('s', time()).'N' .$num;

        mysqli_query($conn, "set names 'utf8'");//写库

        // 检测连接
        if ($conn->connect_error) {
            die("连接失败: " . $conn->connect_error);
        }
        //插入主表SQL
        $sql = "INSERT INTO orderInfo (orderId, customerName,customerCode,customerPhone,deliverGoodsTime,freight,freightAddress,freightPhone,remarks,createTime) 
            VALUES ( '"
            . $orderId . "','"
            . $customerName . "','"
            . $customerCode . "','"
            . $customerPhone . "','"
            . $deliverGoodsTime . "','"
            . $freight . "','"
            . $freightAddress . "','"
            . $freightPhone . "','"
            . $remarks ."','"
            . $newTime."')";
        //$sql = "SELECT * FROM orderInfo";

      if ($conn->query($sql) == true) {
            $orderFlag = true;

      } else {
            $orderFlag = false;
        }


//        添加椅子表信息
        if(sizeof($chairInfo)==0){

        } else {
            $temp = '';
            for($x=0;$x<count($chairInfo);$x++){
                $temp2 = "('".$orderId."','"
                    .$chairInfo[$x]->type."','"
                    .$chairInfo[$x]->name."','"
                    .$chairInfo[$x]->number."','"
                    .($chairInfo[$x]->color)->value."','"
                    .($chairInfo[$x]->color)->name."','"
                    .($chairInfo[$x]->unit)->value."','"
                    .($chairInfo[$x]->unit)->name."','"
                    .($chairInfo[$x]->unit)->weight."','"
                    .($chairInfo[$x]->board)->value."','"
                    .($chairInfo[$x]->board)->name."')";
                if($x == count($chairInfo)-1 ){
                    $temp = $temp.$temp2;
                } else {
                    $temp = $temp.$temp2.',';
                }

            }
            $chairSQL = "INSERT INTO chairInfo (orderId,chairValue,chairName,chairNumber,colorValue,colorName,unitValue,unitName,unitWeight,boardValue,boardName) VALUES ".$temp2;

            if($conn->query($chairSQL) == true){
                $chairFlag = true;

            } else {
                $chairFlag = false;
            }
        }

//        添加桌子表信息
        if(sizeof($tableInfo)==0){

        } else {
            $temp = '';
            for($x=0;$x<count($tableInfo);$x++){
                $temp3 = "('".$orderId."','"
                    .$tableInfo[$x]->type."','"
                    .$tableInfo[$x]->name."','"
                    .$tableInfo[$x]->number."','"
                    .($tableInfo[$x]->size)->value."','"
                    .($tableInfo[$x]->size)->name."','"
                    .($tableInfo[$x]->color)->value."','"
                    .($tableInfo[$x]->color)->name."','"
                    .($tableInfo[$x]->footerAppearance)->value."','"
                    .($tableInfo[$x]->footerAppearance)->name."','"
                    .($tableInfo[$x]->footerMaterial)->value."','"
                    .($tableInfo[$x]->footerMaterial)->name."','"
                    .$tableInfo[$x]->desktopType."','"
                    .$tableInfo[$x]->desktopName."')";
                if($x == count($chairInfo)-1 ){
                    $temp = $temp.$temp3;
                } else {
                    $temp = $temp.$temp3.',';
                }

            }
            $tableSQL = "INSERT INTO tableInfo (orderId,type,name,number,sizeValue,sizeName,colorValue,colorName,footerAppearanceValue,footerAppearanceName,footerMaterialValue,
                                                footerMaterialName,desktopType,desktopName) VALUES ".$temp3;

            if($conn->query($tableSQL) == true){
                $tableFlag = true;

            } else {
                $tableFlag = false;
            }
        }


        //        添加茶几表信息
        if(sizeof($teaTableInfo)==0){

        } else {
            $temp = '';
            for($x=0;$x<count($teaTableInfo);$x++){
                $temp4 = "('".$orderId."','"
                    .$teaTableInfo[$x]->type."','"
                    .$teaTableInfo[$x]->name."','"
                    .$teaTableInfo[$x]->number."','"
                    .($teaTableInfo[$x]->size)->value."','"
                    .($teaTableInfo[$x]->size)->name."','"
                    .($teaTableInfo[$x]->color)->value."','"
                    .($teaTableInfo[$x]->color)->name."','"
                    .$teaTableInfo[$x]->desktopType."','"
                    .$teaTableInfo[$x]->desktopName."')";
                if($x == count($teaTableInfo)-1 ){
                    $temp = $temp.$temp4;
                } else {
                    $temp = $temp.$temp4.',';
                }

            }

            $teaTableSQL = "INSERT INTO teaTableInfo (orderId,type,name,number,sizeValue,sizeName,colorValue,colorName,desktopType,desktopName) VALUES ".$temp4;
            if($conn->query($teaTableSQL) == true){
                $teaTableFlag = true;
            } else {
                $teaTableFlag = false;
            }
        }

        //        添加电视柜表信息
        if(sizeof($tvTableInfo)==0){

        } else {
            $temp = '';
            for($x=0;$x<count($tvTableInfo);$x++){
                $temp5 = "('".$orderId."','"
                    .$tvTableInfo[$x]->type."','"
                    .$tvTableInfo[$x]->name."','"
                    .$tvTableInfo[$x]->number."','"
                    .($tvTableInfo[$x]->size)->value."','"
                    .($tvTableInfo[$x]->size)->name."','"
                    .($tvTableInfo[$x]->color)->value."','"
                    .($tvTableInfo[$x]->color)->name."','"
                    .$tvTableInfo[$x]->desktopType."','"
                    .$tvTableInfo[$x]->desktopName."')";
                if($x == count($tvTableInfo)-1 ){
                    $temp = $temp.$temp5;
                } else {
                    $temp = $temp.$temp5.',';
                }

            }

            $teaTableSQL = "INSERT INTO tvTableInfo (orderId,type,name,number,sizeValue,sizeName,colorValue,colorName,desktopType,desktopName) VALUES ".$temp5;
            if($conn->query($teaTableSQL) == true){
                $tvTableFlag = true;
            } else {
                $tvTableFlag = false;
            }
        }

        //断开连接
        $conn->close();

        //  生成返回结果
        $outText = '';
        if($orderFlag==true){
            $orderFlag===true?$outText = $outText.'订单，':'';
            $chairFlag===true?$outText = $outText.'餐椅，':'';
            $tableFlag===true?$outText = $outText.'餐桌，':'';
            $teaTableFlag===true?$outText = $outText.'茶几，':'';
            $tvTableFlag===true?$outText = $outText.'电视柜，':'';

            $outObj->success = true;
            $outObj->message = '添加 '.$outText.'信息成功!';
            $outObj->data = '';
            echo json_encode($outObj,JSON_UNESCAPED_UNICODE);
        } else {
            $orderFlag===false?$outText = $outText.'订单，':'';
            $chairFlag===false?$outText = $outText.'餐椅，':'';
            $tableFlag===false?$outText = $outText.'餐桌，':'';
            $teaTableFlag===false?$outText = $outText.'茶几，':'';
            $tvTableFlag===false?$outText = $outText.'电视柜，':'';

            $outObj->success = false;
            $outObj->message = '添加 '.$outText.'信息失败！';
            $outObj->data = '';
            echo json_encode($outObj,JSON_UNESCAPED_UNICODE);
        }

    }
?>